package com.fita.wechat.entity.search;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

import java.util.Map;

/**
 * @description:
 * @author: 11200
 * @date: 2019/7/1
 */
public class TransactionRecordSearch implements QueryBuilder {

    private QueryFilter queryFilter;

    private QueryOrder queryOrder;

    private Long userWalletId;

    private Long agentId;

    /**
     * get prepared filter
     *
     * @param refresh whether to re-create filter
     * @return
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
        if (queryFilter == null || refresh) {

            queryFilter = new QueryFilter();

            queryFilter.addEquals("targetUserWallet.id", userWalletId);

            queryFilter.addEquals("targetUserWallet.agent.id", agentId);
        }
        return queryFilter;
    }

    /**
     * get order
     *
     * @return
     */
    public QueryOrder getOrder() {
        if (queryOrder == null){
            queryOrder = new QueryOrder("id", "DESC");
        }
        return queryOrder;
    }

    /**
     * build query string template (sql or hql with :keys)
     *
     * @return
     */
    public String buildSentenceTemplate() {
        return "FROM TransactionRecord WHERE disabled = false AND (distributionRewardRecord IS NULL OR distributionRewardRecord.order.status != 'CREATED') " + getPreparedFilter(false).clauseSentence() + getOrder().sentence();
    }

    /**
     * build query string template corresponding parameters
     *
     * @return
     */
    @Override
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }

    /**
     *
     * @param userWalletId
     * @return
     */
    public TransactionRecordSearch addUserWalletId(Long userWalletId){
        this.userWalletId = userWalletId;
        return this;
    }

    /**
     *
     * @param agentId
     * @return
     */
    public TransactionRecordSearch addAgentId(Long agentId){
        this.agentId = agentId;
        return this;
    }

    public Long getUserWalletId() {
        return userWalletId;
    }

    public void setUserWalletId(Long userWalletId) {
        this.userWalletId = userWalletId;
    }

    public Long getAgentId() {
        return agentId;
    }

    public void setAgentId(Long agentId) {
        this.agentId = agentId;
    }
}
